package com.example.model_service.vo;

import lombok.Data;

@Data
public class Result<T> {

    private Integer code;

    private String msg;

    private T data;

    // 封装返回的数据
    public static <T> Result<T> build(T body, Integer code){
        Result<T> result = new Result<>();
        if(body!=null) {
            result.setData(body);
        }

        result.setCode(code);
        return result;
    }

    // 带参数的成功返回值
    public static<T> Result<T> success(T data){
        return build(data, 200);
    }

    // 不带参数的成功返回值
    public static<T> Result<T> success(){
        return build(null, 200);
    }

    // 不带参数得失败返回值
    public static <T> Result<T> fail(){
        return build(null, 201);
    }

    // 带参数的失败返回值
    public static <T> Result<T> fail(T data){
        return build(data, 201);
    }

    public Result<T> message(String msg){
        this.setMsg(msg);
        return this;
    }
}
